<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Tree;
use think\Cookie;
use think\Hook;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'mall';

    public function _initialize()
    {
        parent::_initialize();
        /*$auth = $this->auth;
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });

        $user = \app\common\model\User::get(1);
        $this->auth->direct($user->id);*/

        //获取brand BEGIN
        $mapBrand = [
            'switch' => 1,
        ];
        $brandData = model('app\common\model\Brand')
            ->field('id,name,image')
            ->where($mapBrand)
            ->order('top_switch desc,weigh asc')
            ->select();

        //$brand = addWebSiteUrl($brandData, ['image']);
        $this->view->assign('brand',$brandData);
        //获取brand END

        //cart BEGIN
        $cart = Cookie::has('mall_cart') ? json_decode(Cookie::get('mall_cart'),true) : [];
        $this->view->assign('cart',$cart);
        //cart END

        //获取商品cate BEGIN
        $tree = Tree::instance();
        $tree->init(collection( model('app\common\model\Category')->where('type','goods')->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $category = $tree->getTreeArray(0);
        $this->view->assign('category',$category);
        //获取商品cate END
    }

    public function index()
    {
        //获取top置顶商品 BEGIN
        $mapTop = [
            'market_switch' => 1,
            'top_switch' => 1,
        ];
        $topGoodsData = model('app\common\model\Goods')
            ->field('id,name,brief,price,costprice,mktprice,image,stock')
            ->where($mapTop)
            ->order('weigh asc')
            ->select();
        //$topGoods = addWebSiteUrl($topGoodsData, ['image']);
        $this->view->assign('topGoods',$topGoodsData);
        //获取top置顶商品 END

        //获取new商品 BEGIN
        $mapNew = [
            'market_switch' => 1,
        ];
        $newGoodsData = model('app\common\model\Goods')
            ->field('id,name,brief,price,costprice,mktprice,image,stock')
            ->where($mapNew)
            ->order('updatetime desc')
            ->limit(10)
            ->select();
        //$newGoods = addWebSiteUrl($newGoodsData, ['image']);
        $this->view->assign('newGoods',$newGoodsData);
        //获取new商品 END

        //banner BEGIN
        $mapBanner = [
            'switch' => 1,
        ];
        $banner = model('app\common\model\Banner')
            ->where($mapBanner)
            ->order('weigh desc')
            ->select();
        $this->view->assign('banner',$banner);
        //banner END


        return $this->view->fetch();
    }

    public function about()
    {
        return $this->view->fetch();
    }

    public function contact()
    {
        return $this->view->fetch();
    }

    public function policy()
    {
        return $this->view->fetch();
    }

    public function faqs()
    {
        return $this->view->fetch();
    }

    public function refund()
    {
        return $this->view->fetch();
    }

    public function logistics()
    {
        return $this->view->fetch();
    }

    public function servise()
    {
        return $this->view->fetch();
    }

    public function intellectual()
    {
        return $this->view->fetch();
    }

}
